package com.example.paging.ui

import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.annotation.LayoutRes
import androidx.databinding.DataBindingUtil
import androidx.databinding.ObservableBoolean
import androidx.paging.LoadState
import androidx.paging.LoadStateAdapter
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.example.paging.R
import com.example.paging.databinding.ItemLoadmoreBinding
import com.example.paging.entity.HomeVO

class HomePageAdapter(context: Context) :
    PagingDataAdapter<PagingItemView<Any>, RecyclerView.ViewHolder>(
        object : DiffUtil.ItemCallback<PagingItemView<Any>>() {
            override fun areItemsTheSame(
                oldItem: PagingItemView<Any>,
                newItem: PagingItemView<Any>
            ): Boolean {
                return oldItem.areItemsTheSame(newItem)
            }

            override fun areContentsTheSame(
                oldItem: PagingItemView<Any>,
                newItem: PagingItemView<Any>
            ): Boolean {
                return oldItem.areContentsTheSame(newItem)
            }

        }) {


    private val layoutInflater: LayoutInflater by lazy {
        LayoutInflater.from(context)
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (position != RecyclerView.NO_POSITION) {
            getItem(position)?.onBindView(holder = holder as PagingViewHolder, position = position)
        }
    }

    override fun getItemViewType(position: Int): Int {
        return getItem(position)!!.layoutRes
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return PagingViewHolder(layoutInflater.inflate(viewType, parent, false))
    }
}

abstract class PagingItemView<T : Any>(@LayoutRes val layoutRes: Int) {

    private lateinit var holder: PagingViewHolder

    open fun onBindView(holder: PagingViewHolder, position: Int) {
        this.holder = holder
    }

    abstract fun areItemsTheSame(data: T): Boolean

    abstract fun areContentsTheSame(data: T): Boolean
}

class ItemArticleView(private val entity: HomeVO.Data.Datas) :
    PagingItemView<ItemArticleView>(R.layout.item_article) {


    override fun areItemsTheSame(data: ItemArticleView): Boolean {
        return entity.id == data.entity.id
    }

    override fun areContentsTheSame(data: ItemArticleView): Boolean {
        return entity.id == data.entity.id
    }

    override fun onBindView(holder: PagingViewHolder, position: Int) {
        super.onBindView(holder, position)
        val textView = holder.itemView.findViewById<TextView>(R.id.textview)
        textView.text = entity.title
        textView.setOnClickListener {
            Log.d("TAG", entity.link)
            WebActivity.activityStart(it.context, entity.link)
        }
    }
}

class LoadMoreAdapter(private val callback: () -> Unit) : LoadStateAdapter<LoadMoreView>() {
    override fun onBindViewHolder(holder: LoadMoreView, loadState: LoadState) {
        holder.bindState(loadState)
    }

    override fun onCreateViewHolder(parent: ViewGroup, loadState: LoadState): LoadMoreView {
        return LoadMoreView(parent, loadState, callback)
    }

}

class LoadMoreView(parent: ViewGroup, loadState: LoadState, val callback: () -> Unit) :
    RecyclerView.ViewHolder(
        LayoutInflater.from(parent.context)!!.inflate(R.layout.item_loadmore, parent, false)
    ) {

    val loading = ObservableBoolean()


    init {
        DataBindingUtil.bind<ItemLoadmoreBinding>(itemView)?.itemView = this
        bindState(loadState)
    }

    fun bindState(loadState: LoadState) {
        loading.set(loadState is LoadState.Loading)
    }
}

class PagingViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
